using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Sys;
using net_work.IService.Sys;

namespace net_work.Controllers.Sys;

[Route("api/[controller]")]
[ApiController]
public class PersonController(ILogger<PersonController> logger, IPersonService personService) : ControllerBase
{
    /// <summary>
    /// 获取个人列表
    /// </summary>
    /// <param name="request">分页请求实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("list")]
    [Authorize]
    public async Task<IActionResult> ListPeople([FromBody] PagedRequest request)
    {
        logger.LogInformation("获取个人列表");
        var result = await personService.GetPeoplePagedAsync(request);
        return AjaxResult.Success(result);
    }

    /// <summary>
    /// 获取个人信息
    /// </summary>
    /// <param name="personId">个人ID</param>
    /// <returns>操作结果</returns>
    [HttpGet("get")]
    [Authorize]
    public async Task<IActionResult> GetPersonById([FromQuery] long personId)
    {
        logger.LogInformation("获取个人信息");
        return await personService.SelectByIdAsync(personId) is { } person
            ? AjaxResult.Success(person)
            : AjaxResult.Error();
    }

    /// <summary>
    /// 新增个人
    /// </summary>
    /// <param name="personAddRequest">新增个人实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("add")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> AddPerson([FromBody] PersonAddRequest personAddRequest)
    {
        logger.LogInformation("新增个人");
        var personId = await personService.InsertPerson(personAddRequest);
        return personId > 0
            ? AjaxResult.Success(new { personId })
            : AjaxResult.Error();
    }

    /// <summary>
    /// 修改个人
    /// </summary>
    /// <param name="personUpdateRequest">修改个人实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("update")]
    [Authorize]
    public async Task<IActionResult> UpdatePerson([FromBody] PersonUpdateRequest personUpdateRequest)
    {
        logger.LogInformation("修改个人");
        return await personService.UpdatePerson(personUpdateRequest) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }

    /// <summary>
    /// 删除个人
    /// </summary>
    /// <param name="ids">个人ID</param>
    /// <returns>操作结果</returns>
    [HttpPost("delete")]
    [Authorize(Roles = "ADMIN,SECRET")]
    public async Task<IActionResult> DeletePerson([FromBody] List<long> ids)
    {
        logger.LogInformation("删除个人");
        return await personService.DeletePersonByIds(ids) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }
}